package com.ebt.m.proposal_v2.utils;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Build;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v4.content.PermissionChecker;
import android.util.ArrayMap;

import com.ebt.m.commons.utils.Logger;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * @author archer.qi
 *         Created on 2017/6/15.
 */
public class PermissionUtils {

    /**
     * 检查权限是否启用
     *
     * @param permission
     * @return
     */
    public static boolean checkPermission(Context context, String permission) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            /* 6.0以下无需验证权限*/
            Logger.i("SDK < 6.0 默认开启权限");
            return true;
        }
        if (ContextCompat.checkSelfPermission(context, permission) == PackageManager.PERMISSION_GRANTED) {
            Logger.i(permission + "权限已经启用");
            return true;
        } else {
            Logger.i(permission + "权限已经启禁用");
            return false;
        }
    }

    /**
     * 用于项目编译版本 < 6.0 时的权限判定
     *
     * @param context
     * @return
     */
    public static boolean isPermissionStart(Context context, String permission) {
        return PermissionChecker.checkSelfPermission(context, permission)
                == PermissionChecker.PERMISSION_GRANTED;
    }

    /**
     * 申请权限
     *
     * @param activity
     * @param permissions
     * @param requestCode
     */
    public static void requestPermission(Activity activity, String[] permissions, int requestCode) {
        ActivityCompat.requestPermissions(activity, permissions, requestCode);
    }

    /**
     * 是否显示请求权限
     *
     * @param activity
     * @param requestPermission
     * @return
     */
    public static boolean shouldShowRequestPermission(Activity activity, String requestPermission) {
        return ActivityCompat.shouldShowRequestPermissionRationale(activity, requestPermission);
    }

    /**
     * 跳转到应用程序列表设置页面
     *
     * @param context
     */
    public static void gotoSettings(Context context) {
        Intent intent = new Intent(Settings.ACTION_APPLICATION_SETTINGS);
        context.startActivity(intent);
    }

    /**
     * 获取最顶层的Activity
     *
     * @return
     */
    public static Activity getTopActivity() {
        Activity topActivity = null;
        try {
            Class activityThreadClass = Class.forName("android.app.ActivityThread");
            Method getATMethod = activityThreadClass.getDeclaredMethod("currentActivityThread");
            Field activitiesField = activityThreadClass.getDeclaredField("mActivities");
            Object activityThread = getATMethod.invoke(null);
            activitiesField.setAccessible(true);

            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
                ArrayMap activities = (ArrayMap) activitiesField.get(activityThread);
                if (activities.size() > 0) {
                    Object activityClientRecord = activities.valueAt(0);

                    Class activityClientRecordClass = Class.forName("android.app.ActivityThread$ActivityClientRecord");
                    Field activityField = activityClientRecordClass.getDeclaredField("activity");
                    activityField.setAccessible(true);
                    topActivity = (Activity) activityField.get(activityClientRecord);
                }
            }

        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (NoSuchFieldException e) {
            e.printStackTrace();
        }
        return topActivity;
    }


}
